package cn.iocoder.yudao.module.nmkj.service.serviceintroduce;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.nmkj.controller.admin.serviceintroduce.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.serviceintroduce.ServiceIntroduceDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nmkj.dal.mysql.serviceintroduce.ServiceIntroduceMapper;

import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SERVICE_INTRODUCE_NOT_EXISTS;

/**
 * 服务介绍 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ServiceIntroduceServiceImpl implements ServiceIntroduceService {

    @Resource
    private ServiceIntroduceMapper serviceIntroduceMapper;

    @Override
    public Long createServiceIntroduce(ServiceIntroduceSaveReqVO createReqVO) {
        // 插入
        ServiceIntroduceDO serviceIntroduce = BeanUtils.toBean(createReqVO, ServiceIntroduceDO.class);
        serviceIntroduceMapper.insert(serviceIntroduce);
        // 返回
        return serviceIntroduce.getId();
    }

    @Override
    public void updateServiceIntroduce(ServiceIntroduceSaveReqVO updateReqVO) {
        // 校验存在
        validateServiceIntroduceExists(updateReqVO.getId());
        // 更新
        ServiceIntroduceDO updateObj = BeanUtils.toBean(updateReqVO, ServiceIntroduceDO.class);
        serviceIntroduceMapper.updateById(updateObj);
    }

    @Override
    public void deleteServiceIntroduce(Long id) {
        // 校验存在
        validateServiceIntroduceExists(id);
        // 删除
        serviceIntroduceMapper.deleteById(id);
    }

    private void validateServiceIntroduceExists(Long id) {
        if (serviceIntroduceMapper.selectById(id) == null) {
            throw exception(SERVICE_INTRODUCE_NOT_EXISTS);
        }
    }

    @Override
    public ServiceIntroduceDO getServiceIntroduce(Long id) {
        return serviceIntroduceMapper.selectById(id);
    }

    @Override
    public PageResult<ServiceIntroduceDO> getServiceIntroducePage(ServiceIntroducePageReqVO pageReqVO) {
        return serviceIntroduceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ServiceIntroduceDO> getServiceIntroduceList(ServiceIntroducePageReqVO reqVO) {
        return serviceIntroduceMapper.selectList(reqVO);
    }

}